package tr.edu.yildiz.dap.appeditor;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.part.FileEditorInput;

import tr.edu.yildiz.ce.dap.appagent.model.App;
import tr.edu.yildiz.ce.dap.appagent.model.AppDefinition;
import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.ce.dap.appagent.model.ComponentList;
import tr.edu.yildiz.ce.dap.appagent.service.DapXmlConverter;
import tr.edu.yildiz.dap.appeditor.pages.MainPage;

public class AppEditor extends FormEditor{

	MainPage mainPage = new MainPage(this, "AppEditor", "Application");
	
	@Override
	protected void addPages() {
		try {
			mainPage.setComponentList(read());
			addPage(0, mainPage);
		} catch (PartInitException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
	}

	@Override
	public void doSave(IProgressMonitor arg0) {
		try {
			save("AppProxy","0.0.1" ,mainPage.getDependencyComposite().getComponentList());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void doSaveAs() {
		try {
			save("AppProxy","0.0.1",mainPage.getDependencyComposite().getComponentList());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
	
	private File getFile(){
		IFile file = ((FileEditorInput)getEditorInput()).getFile();
		return new File( file.getWorkspace().getRoot().getLocation().toOSString()+ file.getFullPath().toOSString());
	}
	
	public void save(String name,String version,List cList) throws IOException, JAXBException{
		ComponentList componentList = new ComponentList();
		AppDefinition appDef = new AppDefinition();
		App app = new App();
		app.setName(name);
		app.setVersion(version);
		appDef.setApp(app);
		
		List<Component> sortedComponentList = new ArrayList<Component>();
		for(int i= cList.size()-1 ; i >= 0 ; i--){
			sortedComponentList.add((Component)cList.get(i));
		}
		componentList.getComponent().addAll(sortedComponentList);
		
		appDef.setComponentList(componentList);
		FileWriter writer = new FileWriter(getFile());
		String xml = DapXmlConverter.convertToXML(appDef);
		writer.write(xml);
		writer.close();
	}
	
	public ComponentList read() throws IOException{
		ComponentList componentList;
		try {
			componentList = ((AppDefinition) DapXmlConverter.convertToObject(getFile())).getComponentList();
		} catch (JAXBException e) {
			e.printStackTrace();
			return new ComponentList();
		}
		return componentList;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

}



